package net.bolue.web.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.bolue.console.data.RepInfo;
import net.bolue.console.data.model.RepInfoModel;
import net.bolue.console.model.ReportModel;
import net.bolue.sysmng.exception.BolueException;
import net.bolue.sysmng.model.ReportResModel;
import net.bolue.sysmng.tblbean.RepRes;
import net.bolue.web.report.RepConstants;
import net.bolue.web.util.RequestUtil;

/**
 * 报表页面
 * @author Administrator
 *
 */
public class ReportAction extends AbstractAction{

	@Override
	public ActionResult perform(HttpServletRequest request, HttpServletResponse response) throws BolueException{
		String subSys = RequestUtil.getParamValue(request, RepConstants.REP_SUBSYS_KEY);
		String strRepID = RequestUtil.getParamValue(request, RepConstants.REP_REPID_KEY);
		if(subSys==null || subSys.trim().length()==0 || strRepID==null || strRepID.trim().length()==0){
			throw new BolueException(1,"报表URL中必须提供subSys及repID参数值!");
		}
		
		String page = RequestUtil.getParamValue(request, "page");
		if(page!=null){//通过页码参数判断是显示页面还是显示数据
			return this.dataList(request, response);
		}else{
			return this.init(request, response);
		}
	}
	
	/**
	 * 显示报表页面
	 * @param request
	 * @param response
	 * @return
	 * @throws BolueException
	 */
	public ActionResult init(HttpServletRequest request, HttpServletResponse response)throws BolueException {
		String subSys = RequestUtil.getParamValue(request, RepConstants.REP_SUBSYS_KEY);
		String strRepID = RequestUtil.getParamValue(request, RepConstants.REP_REPID_KEY);
		Integer repID=Integer.parseInt(strRepID);
		
		ActionResult actionResult = new ActionResult();
		RepInfoModel repInfoModel = new RepInfoModel(request);
		RepInfo repInfo = repInfoModel.getRepInfo(subSys, repID);
		if(repInfo!=null){
			actionResult.setData(repInfo.toMap());
		}
		actionResult.setResult(true);
		return actionResult;
	}
	
	/**
	 * 显示报表数据
	 * @param request
	 * @param response
	 * @return
	 * @throws BolueException
	 */
	public ActionResult dataList(HttpServletRequest request, HttpServletResponse response) throws BolueException{
		String subSys = RequestUtil.getParamValue(request, RepConstants.REP_SUBSYS_KEY);
		String strRepID = RequestUtil.getParamValue(request, RepConstants.REP_REPID_KEY);
		
		Integer repID=Integer.parseInt(strRepID);
		ReportResModel reportResModel = new ReportResModel();
		RepRes repRes = reportResModel.getReport(subSys, repID);
		if(repRes==null){
			throw new BolueException(10, "没有找到报表"+subSys+"."+repID+"对应的报表资源!");
		}
		ReportModel reportModel = new ReportModel(request,repRes);
		return reportModel.listData();
	}
	
}
